package com.browsesoft.user;

import java.io.Serializable;
import java.util.Hashtable;
import java.util.LinkedList;

import com.browsesoft.Manager;
import com.browsesoft.ManagerGroup;
import com.browsesoft.dbtools.DBTools;

/**
 * 签名语管理器
 */
public class SignSentencesManger implements Serializable, Manager {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5991900085176078744L;

	/**
	 * 存放SignSentence的ID，用户，语句内容
	 */
	private LinkedList SignSentences = new LinkedList();

	/**
	 * 空实现
	 */
	public void delete(int id) throws Exception {
	}

	/**
	 * 装载签名
	 */
	public void load() throws Exception {
		String[][] sign = DBTools
				.executeQueryWithTableHead("select * from signtable");
		for (int i = 1; i < sign.length; i++) {
			// 生成签名
			SignSentence ss = new SignSentence();
			Hashtable ht = new Hashtable();
			ht = DBTools.getAttributes(sign, i);
			String id = (String) ht.get("id");
			User user = (User) (ManagerGroup.getManagerGroup()
					.getObject((String) ht.get("userid")));
			String content = (String) ht.get("content");
			ss.setAttrbutes(id, content, user);
			// 装载签名
			SignSentences.add(ss);
		}
	}

	/**
	 * 按ID号得到签名语
	 * 
	 * @param id
	 * @return
	 */
	public Object getObject(String id) {
		for (int i = 0; i < SignSentences.size(); i++) {
			SignSentence o = (SignSentence) SignSentences.get(i);
			try {
				if (((String) (o.getAttributes().get("id"))).equals(id)) {
					return o;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 按用户名得到该用户的所有签名
	 * 
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public LinkedList getSsForUser(User user) throws Exception {
		LinkedList result = new LinkedList();
		for (int i = 0; i < SignSentences.size(); i++) {
			SignSentence o = (SignSentence) SignSentences.get(i);
			String id = (String) o.getAttributes().get("userid");
			if (id.equals(user.getID())) {
				result.add(o);
			}
		}
		return result;
	}

	/**
	 * 按ID号删除
	 * 
	 * @param id
	 * @throws Exception
	 */
	public void remove(String id) throws Exception {
		for (int i = 0; i < SignSentences.size(); i++) {
			SignSentence sign = (SignSentence) SignSentences.get(i);
			if (((String) (sign.getAttributes().get("id"))).equals(id)) {
				SignSentences.remove(i);
			}
		}
	}

	/**
	 * 增加一条签名
	 * 
	 * @param s
	 */
	public void add(SignSentence s) {
		this.SignSentences.add(s);
	}

	/**
	 * 删除用户签名
	 */
	public void deleteSsForUser(User user) throws Exception {
		// 得到用户的所有签名
		LinkedList v = this.getSsForUser(user);
		// 对于每一个签名
		for (int i = 0; i < v.size(); i++) {
			SignSentence ss = (SignSentence) v.get(i);
			// 删除签名
			ss.delete();
		}
	}

	public void synchronAdd(String className, Hashtable attributes) {
		// TODO Auto-generated method stub

	}

	public void synchronRemove(String id) {
		// TODO Auto-generated method stub

	}

	public void synchronUpdate(String id, Hashtable attributes) {
		// TODO Auto-generated method stub

	}
}
